﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.WebCommon.Common;
using SMTC.BO.BU;
using SMTC.Model.Domain;
using SMTC.Model.Model;

public partial class BaseInfoManagement_ModifyBuInfo : BasePage
{
    BUProccessServices proc = new BUProccessServices();
    BUQueryServices query = new BUQueryServices();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request["action"] != null)
            {
                ViewState["action"] = Request["action"].ToString();
                if (ViewState["action"].ToString().Equals("add"))
                {
                    this.lbtitle.Text = Resources.Resource.BUInfoAdd; //"BU信息添加";

                }
                else
                {
                    if (Request["BUID"] != null)
                    {
                        ViewState["BUID"] = Request["BUID"].ToString();
                    }
                    this.lbtitle.Text = Resources.Resource.BUInfoMangement;//"BU信息维护";

                    try
                    {
                        BUDomain domain = query.GetBuDomain(ViewState["BUID"].ToString());
                        this.txtBUName.Text = domain.BU.BUName;
                     
                        List<SMT_LineModel> list = domain.LineList;

                        foreach (SMT_LineModel item in list)
                        {
                            if (item != null)
                            {
                                ListItem li = new ListItem(item.LineName, item.LineID + "|" + item.LineCapacity + "|" );
                                this.lbAddedPU.Items.Add(li);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMessage(Resources.Resource.ErrorForGetLine);//"取得生产线列表失败！请联系管理员"
                        return;
                    }
                    lbAddedPU.DataBind();
                }
            }
        }
    }


    protected void btnUpd_OnClick(object sender, EventArgs e)
    {
        BUDomain domain = new BUDomain();
        SMT_BUModel bu = new SMT_BUModel();
        bu.BUName = this.txtBUName.Text;
        bu.ModifyDateTime = DateTime.Now;
        bu.ModifyPerson = this.UserName;
        bu.BUID = ViewState["BUID"] == null ? Guid.NewGuid().ToString() : ViewState["BUID"].ToString();
        domain.BU = bu;
        List<SMT_LineModel> LineList = new List<SMT_LineModel>();
        foreach (ListItem item in lbAddedPU.Items)
        {
            string[] str = item.Value.Split('|');
            SMT_LineModel line = new SMT_LineModel();
            line.BUID = bu.BUID;
            line.LineID = str[0];
            line.LineName = item.Text;
            line.LineCapacity = Convert.ToInt32(str[1]);
            LineList.Add(line);
        }
        domain.LineList = LineList;

        if (ViewState["action"].ToString().Equals("add"))
        {

            if (proc.InsertBU(domain))
            {
                this.ShowMessage(Resources.Resource.BUInfoAddSuccess);//"BU信息添加成功！"

            }
            else
            {
                this.ShowMessage(Resources.Resource.BUInfoAddFailed);//"BU信息添加失败！请稍后重试！"
            }
        }
        else
        {
            if (proc.UpdateBU(domain))
            {
                this.ShowMessage(Resources.Resource.BUInfoUpdSuccess);//"BU信息维护成功！"

            }
            else
            {
                this.ShowMessage(Resources.Resource.BUInfoUpdFailed);//"BU信息维护失败！请稍后重试！"
            }
        }
        this.btnUpd.Visible = false;
        
    }

    protected void btnAddPU_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.HidAddedLine.Value) && this.HidAddedLine.Value.ToLower() != "undefined")
        {
            string[] line = this.HidAddedLine.Value.Split(';');

            if (line.Length == 0) return;
            for (int i = 0; i < line.Length; i++)
            {
                bool flag = true;

                for (int j = 0; j < lbAddedPU.Items.Count; j++)
                {

                    if (lbAddedPU.Items[j].Value == line[i].Split(',')[0].ToString() + "|" + line[i].Split(',')[2].ToString() )
                    {
                        flag = false;

                    }

                }
                if (flag)
                {
                    lbAddedPU.Items.Add(new ListItem(line[i].Split(',')[1].ToString(), line[i].Split(',')[0].ToString() + "|" + line[i].Split(',')[2].ToString()));

                }

            }
        }
    }

    protected void btnRemovePU_Click(object sender, EventArgs e)
    {

        while (lbAddedPU.SelectedIndex != -1)
        {
            lbAddedPU.Items.Remove(this.lbAddedPU.SelectedItem);
        }
        if (lbAddedPU.Items.Count == 0)
        {
            this.HidAddedLine.Value = "";
        }

    }
}